<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>user page</title>

<link href="../../public/css/jquery.treeTable.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../../public/js/jquery.js"></script> 
<script type="text/javascript" src="../../public/js/jquery.treeTable.js"></script>
<script type="text/javascript">
   var value;
   var status;
   //initial tree when the page load
    $(document).ready(function(){
        
        $("#tree").treeTable({
            clickableNodeNames: true
        });
        $("#treeNext").treeTable({
             clickableNodeNames: true
      
       }); 
       $("#treeStat").treeTable({
          clickableNodeNames:true 
       });
       //check page _Post or not
            value='<?php if (isset($_POST['Submit']))
                              {
                               echo true;
                              }
                          else{
                                echo false;
                               }
               ?>';
           status='<?php echo $this->status; ?>';
           //assign value for textbox estimate and stat;
           $("input#estimate").val(<?php echo $this->estimate; ?>);
           $("input#stat").val(<?php echo $this->dayStat; ?>);
           if(value==false){
               
               $("#treeNext").hide();
               $("#treeStat").hide();
               $(".label_text").hide();
               $("#cboMenu").val('Late');
               $("#History").hide();
               
               
               
           }
           else if(value==true)
           {
                    if(status=='Next')
                    {
                        $("#tree").hide();
                        $("#treeStat").hide();
                        $("#treeNext").show();
                        $("#cboMenu").val('Next');
                       
                        var str='Est Problem ForeCast Next';
                       $("#label").html(str);
                      
                        $("input#stat").hide();
                        $("input#estimate").show();
                        $("#History").hide();
                    }
                    else if(status='Stat')
                    {
                        $("#tree").hide();
                        $("#treeNext").hide();
                        $("#treeStat").show();
                        $("#cboMenu").val('Stat');
                       
                        var str='View Last';
                        $("#label").html(str);
                        
                        $("input#stat").show();
                        $("input#estimate").hide();
                        $("#History").hide();
                    }
                  
                          
           }
          
      });         
      
      //handle when the combobox change value
   function Combo_Change(){
       var v_cbo = document.getElementById("cboMenu");
        var v_strUser = v_cbo.options[v_cbo.selectedIndex].value;
           
   
       // alert(strUser);
         if(v_strUser=="Next"){
        $(document).ready(function(){
            $("div#body").show();
            $("#tree").hide();
            $("#treeNext").show();
            $(".label_text").show();
           
            var str='Est Problem ForeCast Next';
            $("#label").html(str);
            $("#treeStat").hide();
            $("input#stat").hide();
            $("input#estimate").show();
            $("#History").hide();
        });
        
    
       }
       else if(v_strUser=="Late"){
           $(document).ready(function(){
               $("div#body").show();
               $("#treeNext").hide();
               $("#tree").show();
              $(".label_text").hide();
               $("#treeStat").hide();
               $("#History").hide();
           });
       }
       else if(v_strUser=="Stat"){
           $(document).ready(function(){
               $("div#body").show();
              $("#tree").hide();
              $("#treeNext").hide();
              $(".label_text").show();
              
              var str='View Last';
              $("#label").html(str);
              $("#treeStat").show();
              $("input#stat").show();
            $("input#estimate").hide();
            $("#History").hide();
           });
       }
       else if(v_strUser=="History"){
          $(document).ready(function(){
              $("#History").show();
              $("div#body").hide();
              
              var XsisClientID='<?php echo $this->XsisClientID; ?>'
             
             $.get('http://localhost/Zend_xsis/public/rep/history')
                        .success(function (data){
                   //reload data on <div>
                   $('#History').html(data);
                }); 
          });
       }

    }
    
    //handle when the button submit click
    function Click(){
      //  var days =document.getElementById("txtday").value;
      
       $(document).ready(function(){
          $("#treeNext").treeTable({
              clickableNodeNames: true
          }); 
          $("#tree").hide();
           $("#treeNext").show();
          
       });
       
        
    }
    function CheckChange(sel) {
        var value = sel.options[sel.selectedIndex].value; 
       // alert(value);
       if(value=="Next"){
        $(document).ready(function(){
            $("#tree").hide();
            $("#treeNext").show();
            $("p").show();
        });
        
    
       }
       else if(value=="Late"){
           $(document).ready(function(){
               $("#treeNext").hide();
               $("#tree").show();
               $("p").hide();
           });
       }
    }
</script>
    
</head>
<body>
    <form name="userpage" method="post" action="<?php echo $this->action; ?>">
      <?php 
            //get data from controller
           $data_late=$this->data_late;
           $data_next=$this->data_next;
           $data_stat=$this->data_stat;
           $est=$this->estimate;
            $level=$this->level;
           
           
         /* 
          * 
          if(isset($_POST['Submit'])){
              $est=$_POST['txtday'];
              
          }
          else{
              $est=0;
          } */
                  
     ?>
    
<?php include "header.phtml"; ?>
    <div id="header">
        <table width="800px" height="120px" >
            <tr>
                <td align="center" width="120px">
                 <select id="cboMenu" name="cboStatus" onchange="Combo_Change()" style="width: 120px;height: 40px;font-size: 30px;background: red" >
                        <option value="Late">Late</option>
                        <option value="Next">Next</option>
                        <option value="Stat">Stat</option>
                        <option value="History">History</option>

                </select>
            </td >
            <td align="center" width="500px"  >
               <h1 id="label" class="label_text"></h1> 
            </td>
            <td align="center" width="50px"  class="text">
                <input type="text" class="label_text" name="txtest" id="estimate" style="width: 35px;height: 50px;font-size: 30px;alignment-baseline: central" />
                <input type="text" class="label_text" name="txtstat" id="stat" style="width: 35px;height: 50px;font-size: 30px;alignment-baseline: central" />
              
            </td>
            <td align="center" width="50px">
                <h1 class="label_text">Days</h1>
            </td>
            <td align="center" width="80px">
                <input type="submit" class="label_text" name="Submit" value="Click" style="width: 50px;height: 30px" onclick="Click()">
            </td>
        </tr>
    </table>
   
   </div>
   


	  <!--<h1>rep Name : <?php echo $this->repname ;?></h1>
          <h2>level: <?php echo $this->level ?></h2>
          <h2>Job : <?php echo $this->job ?></h2>
          
          <a href="<?php echo $this->logout ?>">logout</a> -->
        
   
    <!-- level 4 -->
    <div id="body">
        <table id="tree" >
        <?php if($level==4){ ?>
        <tr id="node-0">
            <td>Name Group</td>
            <td>Shop</td>
            <td>Manager Group</td>
            <td>email Manager Group</td>
        </tr>
            <?php for($i=0;$i<count($data_late);$i++){ 
                //3 biến cho cấp số tầng của tree
                $l=$i+1; $h=1;$p=1; ?>
        
            <!--level 1 -->
            
            <tr id="node-<?php echo $l; ?>" >
                <td><?php echo $data_late[$i]['Desc']; ?> </td>
                <td><?php echo $data_late[$i]['ShopProblem']."/".($data_late[$i]['ShopProblem']+$data_late[$i]['ShopNoProblem']); ?>   </td>
                <td><?php echo $data_late[$i]['repName'] ?></td>
                <td><?php echo $data_late[$i]['email'] ?></td>
            </tr>
            
            <!-- level 2 -->
            <tr id="node-<?php echo $l."-".$h; ?>" class="child-of-node-<?php echo $l; ?>" >
                <td>Name Route</td>
                <td>Number Shop</td>
                
            </tr>
                <?php for($j=0;$j<count($data_late[$i]['route']);$j++){ $h++;  ?>
                    <tr id="node-<?php echo $l."-".$h;  ?>" class="child-of-node-<?php echo $l; ?>" >
                         <td><?php echo $data_late[$i]['route'][$j]['routeDesc']; ?></td>
                         <td><?php echo $data_late[$i]['route'][$j]['ShopProblem']."/".($data_late[$i]['route'][$j]['ShopProblem']+$data_late[$i]['route'][$j]['ShopNoProblem']) ?></td>
                   </tr>
            
                    <!--Level 3 -->
                    <tr id="node-<?php echo $l."-".$h."-".$p; ?>" class="child-of-node-<?php echo $l."-".$h; ?>" width="200">
                         <td>Shop</td>
                        <td>Rules</td>
                        <td>Days</td>
                        <td width="100">Manager</td>
                        <td>Phone</td>
                   </tr>
                   <?php for($k=0;$k<count($data_late[$i]['route'][$j]['shop']);$k++){  ?>
                         <?php if($data_late[$i]['route'][$j]['shop'][$k]['VisitProblem']=="Yes") { $p++; ?>
                        <tr id="node-<?php echo $l."-".$h."-".$p; ?>" class="child-of-node-<?php echo $l."-".$h ?>" >
                           
                            <td><?php echo $data_late[$i]['route'][$j]['shop'][$k]['storeName']; ?></td>
                            <td><?php echo $data_late[$i]['route'][$j]['shop'][$k]['visitFrequency']; ?></td>
                            <td><?php echo $data_late[$i]['route'][$j]['shop'][$k]['lastvisit']; ?></td>
                            <td><?php echo $data_late[$i]['route'][$j]['shop'][$k]['contact']; ?></td>
                            <td><?php echo $data_late[$i]['route'][$j]['shop'][$k]['storePhone']; ?></td>
                            
                        </tr>
                         <?php } ?>
                   <?php } ?>
                    
                    <!-- end level 3-->
                    
                <?php } ?>
                
               <!-- end level 2 -->
            <?php }; ?>
               
             <!-- end level 1 -->
             
             
             <?php }?>
             
             <!-- if level is 3 -->
             <?php if($level==3 || $level==2 || $level==1) { ?>
                <tr id="node-0" >
                <td>Name Route</td>
                <td>Number Shop</td>
                
            </tr>
                <?php for($i=0;$i<count($data_late);$i++){ $l=$i+1;$h=1;  ?>
                    <tr id="node-<?php echo $l;  ?>" >
                         <td><?php echo $data_late[$i]['routeDesc']; ?></td>
                         <td><?php echo $data_late[$i]['ShopProblem']."/".($data_late[$i]['ShopProblem']+$data_late[$i]['ShopNoProblem']) ?></td>
                   </tr>
            
                    
                    <tr id="node-<?php echo $l."-".$h; ?>" class="child-of-node-<?php echo $l; ?>" width="200">
                         <td>Shop</td>
                        <td>Rules</td>
                        <td>Days</td>
                        <td width="100">Manager</td>
                        <td>Phone</td>
                   </tr>
                   <?php for($j=0;$j<count($data_late[$i]['shop']);$j++){  ?>
                         <?php if($data_late[$i]['shop'][$j]['VisitProblem']=="Yes") { $h++; ?>
                        <tr id="node-<?php echo $l."-".$h; ?>" class="child-of-node-<?php echo $l; ?>" >
                           
                            <td><?php echo $data_late[$i]['shop'][$j]['storeName']; ?></td>
                            <td><?php echo $data_late[$i]['shop'][$j]['visitFrequency']; ?></td>
                            <td><?php echo $data_late[$i]['shop'][$j]['lastvisit']; ?></td>
                            <td><?php echo $data_late[$i]['shop'][$j]['contact']; ?></td>
                            <td><?php echo $data_late[$i]['shop'][$j]['storePhone']; ?></td>
                            
                        </tr>
                         <?php } ?>
                   <?php } ?>
                    
                    <!-- end level 3-->
                    
                <?php } ?>
                
             <?php } ?>
                    
             <!-- if level =2 or 1 -->
           <!--  <?php /*  if($level==2 || $level==1){ ?>
                <tr id="node-0">
                         <td>Shop</td>
                        <td>Rules</td>
                        <td>Days</td>
                        <td width="100">Manager</td>
                        <td>Phone</td>
                   </tr>
                   <?php for($i=0;$i<count($data_late);$i++){$l=$i+1;  ?>
                         <?php if($data_late[$i]['VisitProblem']=="Yes") {  ?>
                        <tr id="node-<?php echo $l; ?>"  >
                           
                            <td><?php echo $data_late[$i]['storeName']; ?></td>
                            <td><?php echo $data_late[$i]['visitFrequency']; ?></td>
                            <td><?php echo $data_late[$i]['lastvisit']; ?></td>
                            <td><?php echo $data_late[$i]['contact']; ?></td>
                            <td><?php echo $data_late[$i]['storePhone']; ?></td>
                            
                        </tr>
                         <?php } ?>
                   <?php } ?>
             
             <?php } */?> -->
        </table>
    
         
    <!-- Table for Next task -->
    <table id="treeNext">
        <?php if($level==4){ ?>
        <tr id="node-0">
            <td>Name Group</td>
            <td>Shop Problem</td>
            <td>Manager Group</td>
            <td>email Manager Group</td>
        </tr>
        
        <?php for($i=0;$i<count($data_next);$i++){ 
           //3 biến cho level của tree 
            $level_1=$i+1;
            $level_2=1;
            $level_3=1;
            
            ?>
            <tr id="node-<?php echo $level_1; ?>">
                <td><?php echo $data_next[$i]['Desc']; ?> </td>
                <td><?php echo $data_next[$i]['ShopProblem'] ?></td>
                <td><?php echo $data_next[$i]['repName'] ?></td>
                <td><?php echo $data_next[$i]['email'] ?></td>
            </tr>
                <tr id="node-<?php  echo $level_1."-".$level_2; ?>" class="child-of-node-<?php echo $level_1; ?>">
                   
                    <td>Route Name</td>
                    <td>Shop Problem</td>
                    <td>AVG</td> 
                    
                </tr>
                <?php for($j=0;$j<count($data_next[$i]['route']);$j++){ $level_2++ ?>
                    <tr id="node-<?php echo $level_1."-".$level_2; ?>" class="child-of-node-<?php echo $level_1; ?>">
                        <td><?php echo $data_next[$i]['route'][$j]['routeDesc']; ?></td>
                        <td><?php echo $data_next[$i]['route'][$j]['ShopProblem']; ?></td>
                        <td><?php echo $data_next[$i]['route'][$j]['AVG']; ?></td>
                    </tr>
                        <tr id="node-<?php echo $level_1."-".$level_2."-".$level_3; ?>" class="child-of-node-<?php echo $level_1."-".$level_2; ?>">
                            <td>Shop</td>
                            <td>Due</td>
                            <td>Est</td>
                            <td width="100">Manager</td>
                            <td>Phone</td>
                        </tr>
                    <?php  
                           
                       for($k=0;$k<count($data_next[$i]['route'][$j]['shop']);$k++){ 
                         $level_3++; 
                         if ($data_next[$i]['route'][$j]['shop'][$k]['due']<=$est){   ?>
                          <tr id="node-<?php echo $level_1."-".$level_2."-".$level_3; ?>" class="child-of-node-<?php echo $level_1."-".$level_2; ?>">
                            <td><?php echo $data_next[$i]['route'][$j]['shop'][$k]['storeName']; ?></td>
                            <td><?php echo $data_next[$i]['route'][$j]['shop'][$k]['due']; ?></td>
                            <td><?php echo $data_next[$i]['route'][$j]['shop'][$k]['est']; ?></td>
                            <td><?php echo $data_next[$i]['route'][$j]['shop'][$k]['contact']; ?>
                                <td><?php echo $data_next[$i]['route'][$j]['shop'][$k]['storePhone']; ?></td>
                            
                          </tr>
                         <?php 
                                
                                    }?>
                    <?php  } ?>
                        
             <?php } ?>
        
        <?php } ?>
        <?php } ?>
        <?php if($level==3 || $level==2 || $level==1){ ?>
        <tr id="node-0">
            <td>Route Name</td>
            <td>Shop Problem</td>
            <td>AVG</td>
            
        </tr>
        <?php for($i=0;$i<count($data_next);$i++){
            //two variables for level=3
            $level_1=$i+1;
            $level_2=1;
         ?>
            <tr id="node-<?php echo $level_1 ?>">
                <td><?php echo $data_next[$i]['routeDesc']; ?></td>
                <td><?php echo $data_next[$i]['ShopProblem']; ?></td>
                <td><?php echo $data_next[$i]['AVG'] ?></td>
            </tr>
        <tr id="node-<?php echo $level_1."-".$level_2; ?>" class="child-of-node-<?php echo $level_1; ?>">
            <td>Shop</td>
            <td>Due</td>
            <td>Est</td>
            <td width="100">Manager</td>
            <td>Phone</td>
        </tr>
        <?php for($j=0;$j<count($data_next[$i]['shop']);$j++){ $level_2++;
             if($data_next[$i]['shop'][$j]['due']<=$est){ ?>
        <tr id="node-<?php echo $level_1."-".$level_2; ?>" class="child-of-node-<?php echo $level_1; ?>">
            <td><?php echo $data_next[$i]['shop'][$j]['storeName']; ?></td>
            <td><?php echo $data_next[$i]['shop'][$j]['due']; ?></td>
            <td><?php echo $data_next[$i]['shop'][$j]['est']; ?></td>
            <td><?php echo $data_next[$i]['shop'][$j]['contact']; ?></td>
            <td><?php echo $data_next[$i]['shop'][$j]['storePhone']; ?></td>
            
        </tr>
        
        <?php } } ?>
        <?php } ?>
        <?php } ?>
    </table>
    <table id="treeStat">
        <?php if ($level==4){ ?>
        <tr id="node-0">
            <td>Group Name</td>
            <td>Sum Route</td>
            <td>Sum Rep</td>
            <td>Sum Shop</td>
            <td>Total Visit</td>
            <td>Avg Visit/Rep</td>
            <td>Late Fines</td>
            <td>Avg Late/Shop</td>
            
        </tr>
        <?php for($i=0;$i<count($data_stat);$i++){
            $level_1=$i+1;
            $level_2=1;
            
            
           
         ?>
        <tr id="node-<?php echo $level_1; ?>">
            <td><?php echo $data_stat[$i]['Desc'] ?></td>
            <td><?php echo count($data_stat[$i]['route']); ?></td>
            <td><?php echo $data_stat[$i]['sumRep']; ?></td>
            <td><?php echo $data_stat[$i]['sumShop']; ?></td>
            <td><?php echo $data_stat[$i]['sumVisit']; ?></td>
            <td><?php ($data_stat[$i]['sumRep']==0)?printf("%01.2f",0): printf("%01.1f",$data_stat[$i]['sumVisit']/$data_stat[$i]['sumRep']); ?></td>
            <td><?php echo $data_stat[$i]['sumLateFine']; ?></td>
            <td><?php ($data_stat[$i]['sumShop']==0)?printf("%01.2f",0): printf("%01.1f",$data_stat[$i]['sumLateFine']/$data_stat[$i]['sumShop']); ?></td>
            
        </tr>
        <tr id="node-<?php echo $level_1."-".$level_2; ?>" class="child-of-node-<?php echo $level_1; ?>">
             <td>Route Name</td>
             <td>Sum Rep</td>
             <td>Sum Shop</td>
             <td>Total Visit</td>
             <td>Avg Visit/Rep</td>
             <td>Late Fines</td>
             <td>Avg Late/Shop</td>
        </tr>
        <?php for($j=0;$j<count($data_stat[$i]['route']);$j++){ $level_2++; ?>
        <tr id="node-<?php echo $level_1."-".$level_2; ?>" class="child-of-node-<?php echo $level_1; ?>">
            <td><?php echo $data_stat[$i]['route'][$j]['routeDesc']; ?></td>
             <td><?php echo $data_stat[$i]['route'][$j]['sumRep']; ?></td>
             <td><?php echo $data_stat[$i]['route'][$j]['sumShop']; ?></td>
             <td><?php echo $data_stat[$i]['route'][$j]['sumVisit']; ?></td>
             <td><?php ($data_stat[$i]['route'][$j]['sumRep']==0)?printf("%01.2f",0):printf("%01.2f",$data_stat[$i]['route'][$j]['sumVisit']/$data_stat[$i]['route'][$j]['sumRep']); ?></td>
             <td><?php echo $data_stat[$i]['route'][$j]['sumLateFine']; ?></td>
             <td><?php ($data_stat[$i]['route'][$j]['sumShop']==0)?  printf("%01.2f",0) : printf("%01.2f",$data_stat[$i]['route'][$j]['sumLateFine']/$data_stat[$i]['route'][$j]['sumShop']);  ?></td>
        </tr> 
        
        <?php } ?>
        <?php } ?>
        <?php } ?>
        
        <?php if($level==3 || $level==2 || $level==1) { ?>
        <tr id="node-0">
            
             <td>Route Name</td>
             <td>Sum Rep</td>
             <td>Sum Shop</td>
             <td>Total Visit</td>
             <td>Avg Visit/Rep</td>
             <td>Late Fines</td>
             <td>Avg Late/Shop</td>
        </tr>
        <?php for($i=0;$i<count($data_stat);$i++){ 
            $level_1=$i+1; ?>
        <tr id="node-<?php echo $level_1; ?>">
            <td><?php echo $data_stat[$i]['routeDesc']; ?></td>
             <td><?php echo $data_stat[$i]['sumRep']; ?></td>
             <td><?php echo $data_stat[$i]['sumShop']; ?></td>
             <td><?php echo $data_stat[$i]['sumVisit']; ?></td>
             <td><?php ($data_stat[$i]['sumRep']==0)?printf("%01.2f",0):printf("%01.2f",$data_stat[$i]['sumVisit']/$data_stat[$i]['sumRep']); ?></td>
             <td><?php echo $data_stat[$i]['sumLateFine']; ?></td>
             <td><?php ($data_stat[$i]['sumShop']==0)?  printf("%01.2f",0) : printf("%01.2f",$data_stat[$i]['sumLateFine']/$data_stat[$i]['sumShop']);  ?></td>
        </tr>
        <?php }?>
        <?php } ?>
    </table>
    
    </div>
   
    <div id="History">
        <?php include 'history.phtml'; ?>
    </div>
<?php include "footer.phtml"; ?>
    </form>
</body>
</html>